#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

int main()
{
  pid_t pd = fork();
  if (pd == 0)
  {
     // 子进程
     int cunt = 5;
     while (cunt)
     {
     cout << "子进程；" << cunt << endl;
       cunt--; 
       sleep(1);
     }
     exit(11);
  }else
  {
    // 父进程
   // int return_code;
   // waitpid(pd, &return_code, 0);
   // printf("是父进程，返回码:%d,错误信息:%d\n", (return_code >> 8)&0xff,return_code &0x7f);
    int status = 0;
    int result = waitpid(pd, &status, 0);
  if(result)
  {
    //成功退出，打印退出码
    if (WIFEXITED(status))
    {
    cout << "成功退出"<< endl;
    cout << WEXITSTATUS(status) << endl; 
    }else{
    // 进程未成功退出，打印退出码
    cout << "没有成功退出，打印退出码:" <<endl;
  }
  }
  }
  return 0;
}





